﻿using System;
using System.Globalization;
using AzukiMap.Layer;
using AzukiMap.Map;
using AzukiMap.Tools.Logger;

namespace AzukiMap.Request
{
    public class PostGisRasterIndexRequest : PostGisVectorRequest
    {
        string _indexColumn;
        private readonly NumberFormatInfo _nfi = new NumberFormatInfo() {NumberDecimalSeparator = "."};

        public string IndexColumn
        {
            get { return _indexColumn; }
            set { _indexColumn = value; }
        }


        public PostGisRasterIndexRequest(PostGisLayer layer, string indexColumn)
            : base(layer)
        {
            Logger.Debug(
                "PostGisRasterIndexRequest.PostGisRasterIndexRequest(2):"
                + " vectorLayer.Name = " + layer.LayerName
                );

            init(layer, indexColumn);
        }

        public PostGisRasterIndexRequest(PostGisLayer layer, string getColumnDataUri, string indexColumn)
            : base(layer, getColumnDataUri)
        {
            Logger.Debug(
                "PostGisRasterIndexRequest.PostGisRasterIndexRequest(3):"
                + " vectorLayer.Name = " + layer.LayerName
                );

            init(layer, indexColumn);
        }

        void init(PostGisLayer layer, string indexColumn)
        {
            _indexColumn = indexColumn;
            init(layer);
        }

        internal override void SetRequestParams()
        {
            SetMapParams();
            try
            {
                _params["table"] = _layer.PgSource.TableName;
                _params["column"] = _layer.PgSource.GeometryColumn;
                _params["db"] = _layer.PgSource.DbName;
                _params["cr"] = _currentExtentWkt;
                _params["pr"] = _previousExtentWkt;
                _params["tolerance"] = _tolerance.ToString(_nfi);
                _params["ats"] = _indexColumn;
                _params["srid"] = _srid.ToString();
                _params["tsrid"] = _layer.PgSource.Srid.ToString();
                _params["f"] = _layer.PgSource.Where;
                
            }
            catch (ArgumentNullException ex)
            {
                Logger.Error("MapStatus or LayerStatus may be null.", ex);
            }
            catch (Exception ex)
            {
                Logger.Error("Something wrong to create a request params.", ex);
            }

            RequestParams = _params;

            Logger.Debug(
                "PostGisDataRequest.SetRequestParams(): "
                + " RequstParams = " + RequestParams.ToString()
                );
        }


    }
}
